package org.springblade.oa.service.impl;

import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.oa.entity.Message;
import org.springblade.oa.mapper.MessageMapper;
import org.springblade.oa.service.IMessageService;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class MessageServiceImpl extends BaseServiceImpl<MessageMapper,Message> implements IMessageService {
	MessageMapper msg;

	private IUserClient userClient;

	@Override
	public List<Map<String, Object>> getData(Map<String, Object> map) {
		return msg.getData(map);
	}

	/**
	 * 获取用户信息
	 *
	 * @param user
	 * @return
	 */
	private User getUser(BladeUser user) {
		R<UserInfo> userInfo = userClient.userInfo(user.getUserId().longValue());
		return userInfo.getData().getUser();
	}

	/**
	 * 修改
	 * zhangxl 2020-02-05
	 *
	 * @return boolean
	 */
	@Override
	public Boolean update(Message msge) {
		msge = formatMsg(msge);

		Integer num = msg.update(msge);

		return num != null && num != 0 ? true : false;
	}
	/**getMsg
	 * 删除
	 * zhangxl 2020-02-05
	 *
	 * @return boolean
	 */
	@Override
	public Boolean delete(Integer id) {
		Integer num = msg.delete(id);

		return num != null && num != 0 ? true : false;
	}

	/**
	 * 根据id 获取 信息
	 * zhangxl 2020-02-05
	 */
	@Override
	public Message getMsgById(Integer id) {
		return msg.getMsgById(id);
	}

	/**
	 * 获取消息列表
	 * type 0:待办任务，1：消息提醒，2：系统消息
	 */
	@Override
	public List<Map<String, Object>> getMsg(BladeUser user, String type, String limit) throws Exception {
		//登录人权限
		User u = getUser(user);
		String uid = u.getId()+"";//用户id
//		String role = u.getRoleId();//角色id
		String bmid =u.getDeptId();//部门id s
		String js = "";
		String role  = user.getRoleName();
		if(role.indexOf("部门报销审批人") != -1){
			bmid= u.getDpprovalDeptId();
			js = "11";
		}else if(role.indexOf("会计") != -1){
			js = "4";
		}else if(role.indexOf("出纳") != -1){
			js = "5";
		}
		if (role == null || "".equals(role))
			throw new Exception("登录人角色为空！！！");

		if (bmid == null || "".equals(bmid))
			throw new Exception("登录人部门为空！！！");

		String[] bmids = bmid.split(",");

		StringBuilder bm = new StringBuilder();
		for (int i = 0; i < bmids.length; i++) {
			bm.append("'|" + bmids[i] + "#" + js + "|'");
			if (i < bmids.length - 1)
				bm.append(",");
		}
		bm.append(",'" + uid + "'");
//		if (!"1".equals(type))
//			bm = new StringBuilder("'" + uid + "'");

		/*** 查询数据 **********************************/
		Map<String, Object> cmp = new HashMap<>();
		cmp.put("xxlx", type);//消息类型（1.任务 2.消息 3.系统通知）
		cmp.put("jsrfw", bm.toString());//接收人范围
		cmp.put("limit", limit);//分页
		return msg.getMsgData(cmp);
	}

	/**
	 * 获取消息 总数
	 */
	@Override
	public Map<String, Object> getMsgCount(BladeUser user) throws Exception {
		//登录人权限
		User u = getUser(user);
		String uid = u.getId()+"";//用户id
//		String role = u.getRoleId();//角色id
		String bmid = u.getDeptId() ;//部门id s
		String js = "";
		String role  = user.getRoleName();
		if(role.indexOf("部门报销审批人") != -1){
			 bmid = u.getDpprovalDeptId();
             js = "11";
		}else if(role.indexOf("会计") != -1){
			 js = "4";
		}else if(role.indexOf("出纳") != -1){
			js = "5";
		}

		if (role == null || "".equals(role))
			throw new Exception("登录人角色为空！！！");

		if (bmid == null || "".equals(bmid))
			throw new Exception("登录人部门为空！！！");

		String[] bmids = bmid.split(",");

		StringBuilder bm = new StringBuilder();
		for (int i = 0; i < bmids.length; i++) {
			bm.append("'|" + bmids[i] + "#" + js + "|'");
			if (i < bmids.length - 1)
				bm.append(",");
		}
		bm.append(",'" + uid + "'");
		/*** 查询数据 **********************************/

		Map<String, Object> cmp = new HashMap<>();
		cmp.put("xxlx", "1");//消息类型（1.任务 2.消息 3.系统通知）
		cmp.put("jsrfw", bm.toString());//接收人范围
		Integer rct = msg.getMsgCount(cmp);//代办任务数量

		cmp.put("xxlx", "2");//消息类型（1.任务 2.消息 3.系统通知）
		cmp.put("jsrfw", "'" + uid + "'");//接收人范围
		Integer xct = msg.getMsgCount(cmp);//代办任务数量

		Map<String, Object> rmp = new HashMap<>();
		rmp.put("count", rct + xct);
		rmp.put("db", rct);
		rmp.put("xx", xct);

		return rmp;
	}


	/**
	 * 新增
	 * zhangxl 2020-02-05
	 *
	 * @return boolean
	 */
//	@Override
//	public Boolean add(Message msge) {
//		msge = formatMsg(msge);
//
//		msge.setId("REPLACE(UUID(),'-','')");//主键id
//		msge.setCjsj("SYSDATE()");
//
//		Integer num = msg.add(msge);
//
//	}

//		return num != null && num != 0 ? true : false;


	/**
	 * message 格式化
	 * zhangxl 2020-02-05
	 */
	private Message formatMsg(Message msg) {
		msg.setId(msg.getId())  ;
		msg.setXtdm(isNull(msg.getXtdm()));
		msg.setYwmk(isNull(msg.getYwmk()));
		msg.setXxlx(isNull(msg.getXxlx()));
		msg.setGlid(isNull(msg.getGlid()));
		msg.setFwlj(isNull(msg.getFwlj()));
		msg.setXxnr(isNull(msg.getXxnr()));
		msg.setScrid(isNull(msg.getScrid()));
		msg.setJsrfw(isNull(msg.getJsrfw()));
		msg.setBz(isNull(msg.getBz()));
		msg.setSfyd(isNull(msg.getSfyd()));
		msg.setZt(isNull(msg.getZt()));
		return msg;
	}

	/**
	 * 是否为 null
	 *
	 * @param str
	 * @return
	 */
	private String isNull(String str) {
		return str == null ? "null" : "'" + str + "'";
	}


}
